// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   CourseScheduleDAO.java

package com.nervytech.tutorcruise.db.dao.mysql.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.stereotype.Repository;

import com.nervytech.tutorcruise.course.beans.CourseDetailsBean;
import com.nervytech.tutorcruise.course.beans.CourseSearchResultBean;
import com.nervytech.tutorcruise.db.dao.interfaces.ICourseScheduleDAO;
import com.nervytech.tutorcruise.db.dao.model.CourseSchedule;

@Repository("courseScheduleDAO")
public class CourseScheduleDAO extends JdbcDaoSupport implements
		ICourseScheduleDAO {

	@Autowired
	CourseScheduleDAO(DataSource dataSource) {
		setDataSource(dataSource);
	}

	public void saveCourseOverview(CourseSchedule courseSchedule) {
		Map argsMap = new HashMap();
		argsMap.put("course_master_id",
				Long.valueOf(courseSchedule.getCourseMasterId()));
		argsMap.put("user_id", Long.valueOf(courseSchedule.getUserId()));
		argsMap.put("location_id", Long.valueOf(courseSchedule.getLocationId()));
		argsMap.put("course_type",
				Integer.valueOf(courseSchedule.getCourseType()));
		argsMap.put("start_date", courseSchedule.getStartDate());
		argsMap.put("end_date", courseSchedule.getEndDate());
		argsMap.put("start_time", courseSchedule.getStartTime());
		argsMap.put("end_time", courseSchedule.getEndTime());
		argsMap.put("fees", Double.valueOf(courseSchedule.getFees()));
		argsMap.put("total_seats",
				Integer.valueOf(courseSchedule.getTotalSeats()));
		argsMap.put("created_time", courseSchedule.getCreatedTime());
		argsMap.put("updated_time", courseSchedule.getUpdatedTime());
		System.out.println((new StringBuilder("IDDDDDDDDDDDDDDDDDDD ")).append(
				argsMap).toString());
		long scheduleId = (new SimpleJdbcInsert(getJdbcTemplate()))
				.withTableName("course_schedule")
				.usingGeneratedKeyColumns(new String[] { "course_schedule_id" })
				.executeAndReturnKey(argsMap).longValue();
		courseSchedule.setCourseScheduleId(scheduleId);
	}

	public void saveCourseOverview(long userId, long courseId,
			long courseScheduleId, String shortUrl) {
		String saveUserQuery = "update course_schedule set tinyurl=?  where course_schedule_id=? and course_master_id and user_id=?";
		Object parms[] = { shortUrl, Long.valueOf(courseScheduleId),
				Long.valueOf(courseId), Long.valueOf(userId) };
		getJdbcTemplate().update(saveUserQuery, parms);
	}

	public List getCourseDetails(long courseId) {
		Object parms[] = { Long.valueOf(courseId) };
		RowMapper mapper = new RowMapper() {

			public CourseDetailsBean mapRow(ResultSet rs, int rowNum)
					throws SQLException {
				CourseDetailsBean courseDetails = new CourseDetailsBean();
				courseDetails.setAgenda(rs.getString("agenda"));
				courseDetails.setCategory(rs.getString("category"));
				courseDetails.setCity(rs.getString("city"));
				courseDetails.setCountry(rs.getString("city"));
				courseDetails.setCourseType(rs.getString("course_type"));
				courseDetails.setDateFrom(rs.getString("start_date"));
				courseDetails.setDateTo(rs.getString("end_date"));
				courseDetails.setFees(rs.getString("fees"));
				courseDetails.setObjective(rs.getString("objective"));
				courseDetails.setOverview(rs.getString("overview"));
				courseDetails.setPreRequisite(rs.getString("prerequisite"));
				courseDetails.setTotalSeats(rs.getInt("total_seats"));
				courseDetails.setTotalRegistered(rs.getInt("total_registered"));
				courseDetails.setState(rs.getString("city"));
				courseDetails.setTargetAudience(rs.getString("audience"));
				courseDetails.setTimeFrom(rs.getString("start_time"));
				courseDetails.setTimeTo(rs.getString("end_time"));
				courseDetails.setTitle(rs.getString("title"));
				courseDetails
						.setTrainerDetails(rs.getString("trainer_details"));
				courseDetails.setTrainerName(rs.getString("full_name"));
				courseDetails.setVenue(rs.getString("address"));
				courseDetails.setZipCode(rs.getString("zip_code"));
				System.out.println((new StringBuilder(
						"Course DetailsSSSSSSSSSSSSSSSSSSSSSS ")).append(
						courseDetails.getOverview()).toString());
				return courseDetails;
			}

		};
		String selectUserQuery = "SELECT cs.start_date,cs.end_date,cs.start_time,cs.end_time,cs.fees,cs.currency, cs.total_seats,cs.total_registered,cs.tinyurl,cm.title,cm.category,cm.level,cm.overview,cm.objective, cm.prerequisite,cm.audience,cm.agenda,cm.trainer_details,cm.tagged_skills,cm.notes, cl.location_name,cl.city,cl.address,cl.zip_code,cs.course_type,u.full_name from course_master cm, course_schedule cs, course_location cl, user u  where cs.course_schedule_id=? and cs.course_master_id=cm.course_master_id and  cs.location_id=cl.course_location_id and cs.user_id=u.user_id";
		logger.debug((new StringBuilder("Select user Query : ")).append(
				selectUserQuery).toString());
		return getJdbcTemplate().query(selectUserQuery, parms, mapper);
	}

	public List getCourseDetails(long userId, Date date) {
		Object parms[] = { Long.valueOf(userId) };
		RowMapper mapper = new RowMapper() {

			public CourseSearchResultBean mapRow(ResultSet rs, int rowNum)
					throws SQLException {
				CourseSearchResultBean course = new CourseSearchResultBean();
				course.setCity(rs.getString("city"));
				course.setCourseId(rs.getLong("course_schedule_id"));
				course.setCourseTitle(rs.getString("title"));
				course.setCourseType(rs.getString("course_type"));
				course.setEndDate(rs.getString("end_date"));
				course.setFee(rs.getString("fees"));
				course.setSkillSets(rs.getString("tagged_skills"));
				course.setState(rs.getString("state"));
				course.setStDate(rs.getString("start_date"));
				course.setTrainerId(rs.getLong("user_id"));
				course.setVenue(rs.getString("address"));
				course.setStTime(rs.getString("start_time"));
				course.setEndTime(rs.getString("end_time"));
				course.setTotalSeats(rs.getInt("total_seats"));
				course.setTotalRegistered(rs.getInt("total_registered"));
				return course;
			}

		};
		String selectUserQuery = "SELECT c.name as city, cs.course_schedule_id, cs.course_type, cs.end_date, cs.start_date, cs.start_time,cs.end_time,cs.fees, cs.total_seats,cs.total_registered, cs.user_id, cm.title, cm.tagged_skills,cl.address, cl.zip_code, s.name as state  from course_master cm, course_schedule cs, course_location cl, city c, state s  where cs.user_id=? and cs.course_master_id=cm.course_master_id and  cs.location_id=cl.course_location_id and cl.city=c.city_id and c.state_id=s.state_id";
		logger.debug((new StringBuilder("Select user Query : ")).append(
				selectUserQuery).toString());
		return getJdbcTemplate().query(selectUserQuery, parms, mapper);
	}
}
